package com.sun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.dao.RoomMapper;
import com.sun.dao.UserMapper;
import com.sun.pojo.Room;
import com.sun.pojo.User;
import com.sun.service.RoomService;
import com.sun.service.UserService;
import com.sun.utils.DataGridViewResult;
import com.sun.utils.PasswordUtil;
import com.sun.utils.SystemConstant;
import com.sun.utils.UUIDutiles;
import com.sun.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public int addUser(User user) {
        user.setSalt(UUIDutiles.getUuid());
        user.setCreateDate(new Date());
        user.setPassword(PasswordUtil.md5(user.getPassword(),user.getSalt(), SystemConstant.PASSWORD_COUNT));
        return userMapper.addUser(user);
    }

    public User checkUserExist(String loginName) {
        return userMapper.findUserByLoginName(loginName);
    }

    public User login(String loginName, String password) {
        User loginUser = userMapper.findUserByLoginName(loginName);
        if(loginUser!=null){
            String codePassword=PasswordUtil.md5(password,loginUser.getSalt(),SystemConstant.PASSWORD_COUNT);
            if(loginUser.getPassword().equals(codePassword)){
                return loginUser;
            }
        }
        return null;
    }
}
